import { sys } from "cc";

class localData {
    /** 当前存储模块的标签 */
    public TAG = 'UKghbOHfahOIhnc';

    private data = null;
    constructor() {
        this.data = sys.localStorage.getItem(this.TAG);
        if(!this.data) this.data = {};
    }

    private setData(data: any) {
        this.data = data;
    }

    private toJson(_data: string): any {
        let result: any;
        try {
            result = JSON.parse(_data);
        } catch (error) {
            return {}
        }
        return result;
    }

    public save(key: string, value: any) {
        this.data[key] = value;
        sys.localStorage.setItem(this.TAG, JSON.stringify(this.data));
    }

    private static _instance: localData;
    static get instance() {
        if (!this._instance) this._instance = new localData;
        return this._instance;
    }
}

export const ld = localData.instance;